package com.minelx.datamicroscope.core.display;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.minelx.hellopandas.core.DataFrame;

import java.util.List;

public class DisplayTemplate {
    private final JSONObject template;

    public DisplayTemplate(JSONObject template) {
        this.template = template;
    }

    public DataFrame process(DataFrame dataFrame) {
        String method = template.getString("method");
        switch (method) {
            case "drop":
                List<String> droppedItems = template.getJSONArray("items").toJavaList(String.class);
                dataFrame.drop(droppedItems);
                // FIXME wait a moment...   make DataFrame.drop() returns result DataFrame
                return dataFrame;
            case "aggregate":
                List<String> groupBy = template.getJSONArray("groupBy").toJavaList(String.class);
                JSONArray specs = template.getJSONArray("specs");
                return dataFrame.aggregate(groupBy, specs);
            case "retains":
                List<String> retainedColumns = template.getJSONArray("retained").toJavaList(String.class);
                dataFrame.retains(retainedColumns);
                // FIXME wait a moment...   make DataFrame.retains() returns result DataFrame
                return dataFrame;
            default:
                throw new IllegalArgumentException("unexpected method: " + method);
        }
    }
}
